//
//  ZHFRadioViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFRadioViewController.h"

#import "ZHFRadioListCell.h"

#import "GSYResources.h"

#import "ZHFRadioList.h"

#import "ZHFAllList.h"

#import "ZHFRadioHeaderView.h"

#import "ZHFRadioListViewController.h"

@interface ZHFRadioViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong)UITableView *radioTableView;
@property (nonatomic,assign)BOOL isUpLoad;
@property (nonatomic,strong)NSMutableArray *allListArray;
@property (nonatomic,strong) ZHFRadioList *radioList;

@property (nonatomic,strong) ZHFRadioHeaderView *radioHV;

@end

@implementation ZHFRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"电台";
    
    self.radioTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.radioTableView.backgroundColor = [UIColor whiteColor];
    self.radioTableView.delegate = self;
    self.radioTableView.dataSource = self;
    [self.view addSubview:self.radioTableView];
    
    [self.radioTableView registerClass:[ZHFRadioListCell class] forCellReuseIdentifier:@"radioCell"];
    
    _allListArray = [NSMutableArray array];
    
    _radioTableView.rowHeight = 110 * ADAPT_HEIGHT;
    
#pragma mark- 设置headerView
#pragma mark 轮播图
    self.radioHV = [[ZHFRadioHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 310 * ADAPT_HEIGHT)];
    self.radioTableView.tableHeaderView = self.radioHV;
    
    [self getAFNetWorkingData];
}

- (void)getAFNetWorkingData
{
    NSString *urlStr = @"http://api2.pianke.me/ting/radio";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6"];
    
    [GSYResources postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        self.radioList = [[ZHFRadioList alloc] init];
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        [self.radioList setValuesForKeysWithDictionary:bigDic];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"alllist"]) {
            
            ZHFAllList *allList = [[ZHFAllList alloc] init];
            [allList setValuesForKeysWithDictionary:dic];
            [self.allListArray addObject:allList];
        }
        NSMutableArray *caArray = [bigDic objectForKey:@"carousel"];
        [self.radioHV presentToCarouselArray:caArray];
        NSMutableArray *hotArray = [bigDic objectForKey:@"hotlist"];
        [self.radioHV presentToHotListArray:hotArray];
        
        [self.radioTableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"radioCell";
    ZHFRadioListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.allList = _allListArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZHFAllList *allList = [_allListArray objectAtIndex:indexPath.row];
    
    ZHFRadioListViewController *radioListVC = [[ZHFRadioListViewController alloc] init];
    
    radioListVC.titleStr = allList.title;
    radioListVC.radioId = allList.radioid;
    
    [self.navigationController pushViewController:radioListVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
